(function() {
	var ics = Iefgames_CS;
	if ($defined(ics.Message)) {
		return ;
	}
	
	ics.Message = function() {
		
		var HtmlConf = {
			'csWindows': 'cs_windows',
			
			'csWindow': 'cs_window',
			'csHistory': 'cs_history',
			'csInput': 'cs_input',
			'csSend': 'cs_send',
			'csClose': 'cs_close'
		}
		
		var port = 8080;
		var open = function(usUuid) {
			if (!usUuid) {
				usUuid = null;
			}
			
			if (!$(HtmlConf.csWindows)) {
				$(document.body).grab(new Element('div', {
					id: HtmlConf.csWindows
				}));
			}
			
			if (!$(HtmlConf.csWindow + '_' + usUuid)) {
				$(HtmlConf.csWindows).innerHTML += getCsWindowStr(usUuid);
				$(HtmlConf.csSend + '_' + usUuid).addEvent('click', send);
				$(HtmlConf.csClose + '_' + usUuid).addEvent('click', close);
				$(HtmlConf.csInput + '_' + usUuid).addEvent('click', function(event) {
					//TODO: 未测试
					if (event.key == 13) {
						event.stop();
						event.stopPropagation();
						
						send(event.target);
					}
				});
			}
			
			//如果不是cs才开始comet
			if (!ics.isCs) {
				ics._('startComet');
			}
		}
		
		var close = function(ev) {
			//TODO: 未完全测试
			var usUuid = ev.target.id.replace(HtmlConf.csClose + '_', '');
			$(HtmlConf.csWindow + '_' + usUuid).distory();
			
		}
		
		var send = function(ev) {
			var usUuid = null, csUuid = null;
			if (ics.isCs) {
				//可能会出现多个聊天窗口
				usUuid = ev.target.id.replace(HtmlConf.csSend + '_', '');
				csUuid = Cookie.read(ics.Cookie.csUuidCookieName);
			} else {
				csUuid = Cookie.read(ics.Cookie.csUuidCookieName);
				usUuid = Cookie.read(ics.Cookie.usUuidCookieName);
			}
			
			if (!usUuid
					|| usUuid.length != 32
					|| !csUuid
					|| csUuid.length != 32) {
				return;	
			}
			
			//消息为空
			if (!$(HtmlConf.csInput + '_' + (ics.isCs?usUuid:'null')).value.trim()) {
				return;
			}
			
			var ajax = ics.Ajax();
			if (ajax == null) {
				return; // throw exception
			}
			
			ajax.open("POST",  'msg?' + $time(), true);// HTTP 请求的方式, URL, 是否异步
//			ajax.onreadystatechange = stateChange;
			ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8;');		//给servert post方法取参数时使用
			ajax.setRequestHeader('Accept-Charset','utf-8;');		//给servert post方法取参数时使用
			ajax.send('isCs=' + ics.isCs
						+ '&csUuid=' + csUuid
						+ '&usUuid=' + usUuid
						+ '&msg=' + $(HtmlConf.csInput + '_' + (ics.isCs?usUuid:'null')).value.trim());
//						+ '&msg=' + encodeURIComponent(ics._('base64Encode', $(HtmlConf.csInput + '_' + (ics.isCs?usUuid:'null')).value.trim())));
			
			//清空输入框
			$(HtmlConf.csInput + '_' + (ics.isCs?usUuid:'null')).value = '';
			$(HtmlConf.csInput + '_' + (ics.isCs?usUuid:'null')).focus();
		}
		
		var getCsWindowStr = function(usUuid) {
			var str = '';
			str += '<div id="' + HtmlConf.csWindow + '_' + usUuid + '" class="chat">\n';
			str += '\t<div id="' + HtmlConf.csHistory + '_' + usUuid  + '" class="chat_content">\n';
			str += '\t\t<ul>\n';
			str += '\t\t</ul>\n';
			str += '\t</div>\n';
			str += '\t<div class="chat_q">\n';
			str += '\t\t<div class="chat_input">\n';
			str += '\t\t\t<textarea id="' + HtmlConf.csInput + '_' + usUuid + '" rows="2">你好啊</textarea>\n';
			str += '\t\t</div>\n';
			str += '\t\t<div class="chat_button"><button id="' + HtmlConf.csSend + '_' + usUuid + '" title="发送消息">发送</button>\n';
			str += '\t\t<br /><button id="' + HtmlConf.csClose + '_' + usUuid + '" title="关闭当前窗口">关闭</button>\n';
			str += '\t\t</div>\n';
			str += '\t</div>\n';
			str += '</div>\n';
			return str;
		}
		
		var show = function(msgs) {
			if ($type(msgs) == 'array') {
				return msgs.each(show);
			}
			
			if (ics.isCs) {
				//可能是us此次对话首次发来的消息，需要检查是否有聊天窗口存在
				if (!$(HtmlConf.csWindow + '_' + msgs.usUuid)) {
					open(msgs.usUuid);
				}
			}
			
			var historyEl = $(HtmlConf.csHistory + '_' + (ics.isCs?msgs.usUuid:'null'));
			historyEl.innerHTML += json2Html(msgs);
			historyEl.scrollTop += historyEl.scrollHeight;
		}
		
		var json2Html = function(json) {
			return '<li class="t_color12"><strong>' + (json.cs ? "cs" : 'us') +'</strong>:' + json.msg)) + '</li>';
//			return '<li class="t_color12"><strong>' + (json.cs ? "cs" : 'us') +'</strong>:' + decodeURIComponent(ics._('base64Decode', json.msg)) + '</li>';
		}
		
		ics._$('send', send);
		
		ics._$('open', open);
		
		ics._$('msg', show);

		return {
			'open': open
		}
	}()
	
})()